package com.example.cardgame

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity

import androidx.activity.enableEdgeToEdge

const val NAME_MSG ="NAME_MSG"
const val NUMBER_MSG="NUMBER_MSG"
const val PHONE_MSG="PHONE_MSG"
const val EMAIL_MSG="EMAIL_MSG"
const val SEX_MSG="SEX_MSG"
const val HOBBY_MSG="HOBBY_MSG"
const val IMAGE_MSG="IMAGE_MSG"

class MainActivity : ComponentActivity() {

    @SuppressLint("MissingInflatedId")

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.layout)


        val alterButton = findViewById<Button>(R.id.alter_button)
        alterButton.setOnClickListener {
            val intent = Intent(this, Alter::class.java)
            startActivityForResult(intent, 0)
        }


        val gameButton = findViewById<Button>(R.id.game_button)
        gameButton.setOnClickListener {
            val intent = Intent(this, Game::class.java)
            startActivity(intent)
            startActivityForResult(intent, 1) // Use a different request code
        }


    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 0 && resultCode == RESULT_OK) { // 注意这里应该是 requestCode == 0

            val imageBytes = data?.getByteArrayExtra(IMAGE_MSG)
            if (imageBytes != null) {
                val imageBitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
                // 使用 imageBitmap 更新 ImageView
                findViewById<ImageView>(R.id.imageView2).setImageBitmap(imageBitmap)
            }


            // Extract data from intent
            val name = data?.getStringExtra(NAME_MSG) ?: ""
            val number = data?.getStringExtra(NUMBER_MSG) ?: ""
            val phone = data?.getStringExtra(PHONE_MSG) ?: ""
            val email = data?.getStringExtra(EMAIL_MSG) ?: ""
            val sex = data?.getStringExtra(SEX_MSG) ?: ""
            val hobbies = data?.getStringExtra(HOBBY_MSG) ?: ""

            // Update UI or model with new data
            findViewById<TextView>(R.id.text_name).text = name
            findViewById<TextView>(R.id.text_hobby).text = "爱好:" + hobbies
            findViewById<TextView>(R.id.text_phone).text = phone
            findViewById<TextView>(R.id.text_sex).text = "性别:" + sex
            findViewById<TextView>(R.id.text_address).text = email
            findViewById<TextView>(R.id.text_number).text = number

        }
    }

    }
